/*
 * Copyright (c) 1996-2013 Qisda Corporation
 *
 * This software is copyrighted by and is the sole property of Qisda
 * Corporation. All rights, title, ownership, or other interests in the
 * software remain the property of Qisda Corporation. This software may only be
 * used in accordance with the corresponding license agreement.  Any
 * unauthorized use, duplication, transmission, distribution, or disclosure of
 * this software is expressly forbidden.
 *
 * This Copyright notice may not be removed or modified
 * without prior written consent of Qisda Corporation
 *
 * Qisda Corporation reserves the right to modify this software without notice.
 */
package com.jwgf.smarowindingmachine_unity.ui;

import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwgf.common.ScreenOffTimeoutControl;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.Report;

import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;


public class ProtectActivity extends Activity {
    private static final String TAG = "ProtectActivity";
    private RelativeLayout mLayout;
    private ImageView img;
    private TextView production;
    private TextView efficiency;
    private Disposable disposable;

    private ObjectAnimator animator;
    private RelativeLayout content;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.protect_activity_layout);
        mLayout = findViewById(R.id.protectlayout);
        content = findViewById(R.id.content);

        mLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                animator.cancel();
                disposable.dispose();
                finish();
            }
        });
        production = findViewById(R.id.p_value);
        efficiency = findViewById(R.id.e_value);
        if (ScreenOffTimeoutControl.getScreenOffDisplay() == 0) {
            content.setVisibility(View.INVISIBLE);
        }
        DecimalFormat df = new DecimalFormat("0.00");
        String totalBucketWeight = df.format(Report.getInstance().getTotalBucketWeight());
        String totalProductionEfficiency = df.format(Report.getInstance().getTotalProductionEfficiency());
        production.setText(totalBucketWeight + "Kg");
        efficiency.setText(totalProductionEfficiency + "%");


        disposable = Observable.interval(20, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        DecimalFormat df = new DecimalFormat("0.00");

                        final String totalBucketWeight = df.format(Report.getInstance().getTotalBucketWeight());
                        final String totalProductionEfficiency = df.format(Report.getInstance().getTotalProductionEfficiency());

                        production.setText(totalBucketWeight + "Kg");
                        efficiency.setText(totalProductionEfficiency + "%");
                    }
                });

        startAnimation();

    }

    private void startAnimation() {
        // 使用post是为了能够获取到layout的大小位置
        content.post(new Runnable() {
            @Override
            public void run() {
                Log.i("HardwareAcceleration", String.valueOf(content.isHardwareAccelerated()));

                int offset = mLayout.getHeight() - content.getHeight();
                float begin = content.getTranslationY();
                animator = ObjectAnimator.ofFloat(content, "translationY", begin, offset, begin);
                if (Build.VERSION.RELEASE.equals("6.0.1")) {
                    animator.setDuration(15000);
                } else {
                    animator.setDuration(10000);
                }
                animator.setRepeatCount(ValueAnimator.INFINITE);
                animator.start();
            }
        });

    }

}